package cn.yibao.interviewtest.controller;

import cn.yibao.interviewtest.pojo.User;
import cn.yibao.interviewtest.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(description = "用户层")
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;
    @ApiOperation(value = "获取用户列表")
    @GetMapping("/getUsers")
    public String getUsers(Model model){
        List<User> users = userService.getUsers(null);
        System.out.println("users.size():"+users.size());
        for (User user : users) {
            System.out.println(user);
        }
        model.addAttribute("users",users);
        return "User";
    }

    @ApiOperation(value = "存储用户信息")
    @PostMapping("/saveUser/{username}/{password}/{email}")
    @ResponseBody
    public String saveUser(@ApiParam(value = "用户名",required = true) @PathVariable(value = "username") String username,
                           @ApiParam(value = "密码",required = true)@PathVariable(value = "password") String password,
                           @ApiParam(value = "邮箱",required = true)@PathVariable(value = "email") String email){
        System.out.println(username+password+email);
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        int result = userService.saveUser(user);
        if (result>0){
            return "添加成功！";
        }else {
            return "添加失败……";
        }
    }

    @ApiOperation(value = "修改用户信息")
    @RequestMapping("/updateUser")
    public String updateUser(Model model,User user){
        List<User> users = userService.getUsers(user);
        model.addAttribute("users",users);
        return "update";
    }
}
